﻿// (c) Norbert Huffschmid
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.

using System;
using System.Globalization;
using System.Windows.Data;

namespace SpinDotters.Core
{
    public class SpinAngleConverter : IValueConverter
    {
        /// <summary>
        /// Converts a given spin angle according to a given gear ratio
        /// </summary>
        /// <param name="value">Spin angle value</param>
        /// <param name="targetType">Double</param>
        /// <param name="parameter">Gear ratio</param>
        /// <param name="culture">Not applicable</param>
        /// <returns>Converted spin angle</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value.GetType() != typeof(double)) || (targetType != typeof(double)))
                throw new ArgumentException("Invalid SpinAngleConverter arguments");

            double gearRatio = (parameter == null) ? 1.0 :
                Double.Parse(parameter as string, CultureInfo.InvariantCulture);

            return ((double)value * gearRatio);
        }

        /// <summary>
        /// Converts a given spin angle according to a given inverse gear ratio
        /// </summary>
        /// <param name="value">Spin angle value</param>
        /// <param name="targetType">Double</param>
        /// <param name="parameter">Inverse gear ratio</param>
        /// <param name="culture">Not applicable</param>
        /// <returns>Converted spin angle</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value.GetType() != typeof(double)) || (targetType != typeof(double)))
                throw new ArgumentException("Invalid SpinAngleConverter arguments");

            double gearRatio = (parameter == null) ? 1.0 :
                1.0 / Double.Parse(parameter as string, CultureInfo.InvariantCulture);

            return ((double)value * gearRatio);
        }
    }
}
